package com.yy.wga.merchant.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.common.core.dto.PageBean;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.util.mapping.BeansUtils;
import com.yy.wga.merchant.feign.request.MerchantMpConfigPageReq;
import com.yy.wga.merchant.feign.request.MerchantMpConfigSaveReq;
import com.yy.wga.merchant.feign.response.MerchantMpConfigInfoResp;
import com.yy.wga.merchant.service.mapper.MerchantInfoMapper;
import com.yy.wga.merchant.service.pojo.MerchantInfoDO;
import com.yy.wga.merchant.service.service.MerchantInfoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商户信息配置(merchant)表服务实现类
 *
 * @author guochao
 * @since 2024-10-25 21:46:24
 */
@Service
public class MerchantMpConfigServiceImpl extends ServiceImpl<MerchantInfoMapper, MerchantInfoDO> implements MerchantInfoService {
    @Override
    public PageBean<MerchantMpConfigInfoResp> pageData(MerchantMpConfigPageReq request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), true);
        List<MerchantInfoDO> merchantMpConfigList = baseMapper.selectList(new LambdaQueryWrapper<MerchantInfoDO>()
                .eq(StringUtils.isNotBlank(request.getAppId()), MerchantInfoDO::getAppId, request.getAppId())
                .eq(StringUtils.isNotBlank(request.getAppKey()), MerchantInfoDO::getAppKey, request.getAppKey())
                .like(StringUtils.isNotBlank(request.getName()), MerchantInfoDO::getName, request.getName())
                .eq(request.getStatus() != null, MerchantInfoDO::getStatus, request.getStatus()));
        PageBean<MerchantMpConfigInfoResp> pageBean = new PageBean<>();
        pageBean.setPageInfo(merchantMpConfigList);
        pageBean.setList(BeansUtils.mapList(merchantMpConfigList, MerchantMpConfigInfoResp.class));
        return pageBean;
    }

    @Override
    public void saveData(MerchantMpConfigSaveReq request) {
        MerchantInfoDO merchantInfoDO = BeansUtils.map(request, MerchantInfoDO.class);
        if (StringUtils.isBlank(request.getAppId())) {
            baseMapper.insert(merchantInfoDO);
        } else {
            baseMapper.updateById(merchantInfoDO);
        }
    }
}
